// wjg

// //////////////////////////////////////////////////////////
var userCtx = new ModuleContext('User','用户');

var appui = new ModuleUI(userCtx);

appui.checkEditForm = function(){
	var vConf = {};
	//
 	vConf = [
	{"name":"id","format":"Integer","tip":" 格式错误!"}
	];
	var v = new Validator(userCtx.EDIT_FORM_ID, vConf, true);
	var ok = v.validate();
	if(!ok){
		return false;
	}
	return true;
}

appui.setViewItem = function(dataObj, viewObj){
	viewObj.setAttribute('class','row');
	viewObj.setAttribute('id', 'XXXListItem_' + dataObj.id);
	var children = DomUtil.getChildren(viewObj);
	children[0].firstChild.value = dataObj['id'];
	children[1].innerHTML = Widget.getView(dataObj['id'], 'hidden');
	children[2].innerHTML = Widget.getView(dataObj['username'], 'text');
	children[3].innerHTML = Widget.getView(dataObj['password'], 'text');
	children[4].innerHTML = Widget.getView(dataObj['enabled'], 'switch');
	children[5].innerHTML = Widget.getView(dataObj['created'], 'date');
	children[6].innerHTML = Widget.getView(dataObj['modified'], 'date');
	children[7].firstChild.onclick = function(){appui.openRoleSetup(dataObj.id);};
	children[8].firstChild.onclick = function(){appui.openAuthoritySetup(dataObj.id);};
	children[9].firstChild.onclick = function(){appui.editXXX(dataObj.id);};
	children[10].firstChild.onclick = function(){appui.deleteXXX(dataObj.id);};
}

appui.openRoleSetup = function(userId){
	
	function submitSetup(dialog){
		var url = userCtx.URL_PREFIX + '/setting/saveRoles.json';
		var values = {userId:userId, roleId:dialog.getSelectedValue()};
		ajaxRequest(url, values, function(result){
			unpopup();
			alert('设置成功！');
		}, 'post');
	}
	
	function openOptions(values){
		var optionsUrl = URL_ROOT+'/role/select.json';
		if(!appui.optDialog){
			appui.optDialog = new OptionDialog('roleId', optionsUrl, 'id', 'name');
			appui.optDialog.onOk = submitSetup;
		}
		appui.optDialog.popup('设置', values);
	}
	
	var values = {"userId":userId};
	var url_get = userCtx.URL_PREFIX + '/setting/getRoleIdList.json';
	ajaxRequest(url_get, values, function(result){
		openOptions(result.data);
	});

}

appui.openAuthoritySetup = function(userId){
	
	function submitSetup(dialog){
		var url = userCtx.URL_PREFIX + '/setting/saveAuthoritys.json';
		var values = {userId:userId, authorityId:dialog.getSelectedValue()};
		ajaxRequest(url, values, function(result){
			unpopup();
			alert('设置成功！');
		}, 'post');
	}
	
	function openOptions(values){
		var optionsUrl = URL_ROOT+'/authority/select.json';
		var optDialog = new OptionDialog('authorityId', optionsUrl, 'id', 'name');
		optDialog.onOk = submitSetup;
		optDialog.popup('设置', values);
	}
	
	var values = {"userId":userId};
	var url_get = userCtx.URL_PREFIX + '/setting/getAuthorityIdList.json';
	ajaxRequest(url_get, values, function(result){
		openOptions(result.data);
	});

}
EventUtil.addEvent(window, 'load', function(){

		
});


//////////////////////////////////////////////////////////